package com.example.presentshopping.ui.fragment.gener

import BaseRecyclerAdapter
import BaseRecyclerHolder
import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.GenerComFramnetBean
import com.example.presentshopping.config.bean.SpecialOrderBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.net.http.HttpURL.Req_addRefund
import com.example.presentshopping.net.observer.TaskCallback
import com.example.presentshopping.ui.activity.DetailsActivity
import com.example.presentshopping.ui.activity.GroupDetailsActivity
import com.example.presentshopping.ui.activity.OrderDetailsActivity
import com.example.presentshopping.ui.activity.TeamActivity
import com.example.presentshopping.ui.dialog.MessageDialog
import com.example.presentshopping.utils.ToastUtil
import com.example.presentshopping.utils.glide.GlideUtil
import com.example.presentshopping.utils.tool.GsonUtils
import com.yanzhenjie.recyclerview.SwipeRecyclerView

//普通订单
class GenerComFramnet(var is_team: Int, var status: String) : BaseMvpFragment() {

    var page = 1
    var Special_recy: SwipeRecyclerView? = null
    var gGenerComFrBean: GenerComFramnetBean? = null

    var baseRecyclerAdapter: BaseRecyclerAdapter<GenerComFramnetBean.DataBean.ListBean>? = null

    private var queryInfoPresenter: PublicPresenter? = null

    override fun initView(view: View?, savedInstanceState: Bundle?) {
        queryInfoPresenter = PublicPresenter(context, false, "加载中...")
        queryInfoPresenter?.attachView(this)
        Special_recy = view?.findViewById(R.id.recycler_view)
        setAdapter()
        OrderList()

    }

    override fun onResume() {
        OrderList()
        super.onResume()
    }


    fun setAdapter() {
        baseRecyclerAdapter = object : BaseRecyclerAdapter<GenerComFramnetBean.DataBean.ListBean>(
            context,
            null,
            R.layout.iemt_special) {
            override fun convert(
                holder: BaseRecyclerHolder?,
                data: GenerComFramnetBean.DataBean.ListBean,
                position: Int,
                isScrolling: Boolean,
                selectedPosition: Int
            ) {
                val time = holder?.getView<TextView>(R.id.time_tv)
                val title = holder?.getView<TextView>(R.id.title_tv)
                val amount = holder?.getView<TextView>(R.id.amount_tv)
                val number = holder?.getView<TextView>(R.id.number_tv)
                val thumb = holder?.getView<ImageView>(R.id.thumb_iv)
                val statustv = holder?.getView<TextView>(R.id.status_tv)
                val tv_getSMSCode = holder?.getView<TextView>(R.id.tv_getSMSCode)
                val tv_getSMSCode1 = holder?.getView<TextView>(R.id.tv_getSMSCode1)
                val tv_getSMSCode2 = holder?.getView<TextView>(R.id.tv_getSMSCode2)

                val Order_tv = holder?.getView<TextView>(R.id.Order_tv)

                val Order_Dilis = holder?.getView<LinearLayout>(R.id.Order_Dilis)

                val status = data?.status
                val team_status = data?.team_status
                var tvstatus = ""


                GlideUtil.load(context, data?.thumb, thumb)
                amount?.text = " ￥" + data.amount
                number?.text = "X" + data.number
                Order_tv?.text = "订单编号：" + data.id
                time?.text = data.create_time
                title?.text = data.title
                tv_getSMSCode?.text = ""
                when (team_status) {
                    1 -> {
                        tvstatus = "拼团中"
                        tv_getSMSCode?.visibility = View.GONE
                    }
                    2 -> {
                        when (status) {
                            1 -> {
                                tvstatus = "未支付"
                                tv_getSMSCode?.text = "立即支付"
                                tv_getSMSCode?.visibility = View.VISIBLE
                                tv_getSMSCode1?.visibility = View.GONE
                                tv_getSMSCode2?.visibility = View.GONE
                            }
                            2 -> {
                                tvstatus = "待发货"
                                tv_getSMSCode1?.text = "取消订单"
                                tv_getSMSCode?.visibility = View.GONE
                                tv_getSMSCode1?.visibility = View.VISIBLE
                                tv_getSMSCode2?.visibility = View.GONE
                            }

                            3 -> {
                                tvstatus = "待收货"
                                tv_getSMSCode?.text = "确认收货"
                                tv_getSMSCode1?.text = "取消订单"
                                tv_getSMSCode2?.text = "物流信息"
                                tv_getSMSCode?.textSize = 8f
                                tv_getSMSCode1?.textSize = 8f
                                tv_getSMSCode2?.textSize = 8f

                                tv_getSMSCode?.visibility = View.VISIBLE
                                tv_getSMSCode1?.visibility = View.VISIBLE
                                tv_getSMSCode2?.visibility = View.VISIBLE
                            }

                            4 -> {
                                tvstatus = "已取消"
                                tv_getSMSCode?.visibility = View.GONE
                                tv_getSMSCode1?.visibility = View.GONE
                                tv_getSMSCode2?.visibility = View.GONE
                            }

                            5 -> {
                                tvstatus = "拼团成功"
                                tv_getSMSCode?.visibility = View.GONE
                                tv_getSMSCode1?.visibility = View.GONE
                                tv_getSMSCode2?.visibility = View.GONE
                            }

                            6 -> {
                                tvstatus = "拼团失败"
                                tv_getSMSCode?.visibility = View.VISIBLE
                                tv_getSMSCode1?.visibility = View.GONE
                                tv_getSMSCode2?.visibility = View.GONE
                            }

                            7 -> {
                                tvstatus = "已完成"
                                tv_getSMSCode?.text = "物流详情"
                                tv_getSMSCode?.visibility = View.VISIBLE
                                tv_getSMSCode1?.visibility = View.GONE
                                tv_getSMSCode2?.visibility = View.GONE

                            }
                        }
                    }
                }
                statustv?.text = tvstatus

                tv_getSMSCode?.setOnClickListener {
                    if (tv_getSMSCode?.text.contains("确认收货")) {
                        Setstatus(data.id.toString())
                    } else {
                        if (status == 1) {
                            val Inttem = Intent(context, TeamActivity().javaClass)
                            Inttem.putExtra("order_id", data.id)
                            context?.startActivity(Inttem)
                        }
                    }
                }
                tv_getSMSCode1?.setOnClickListener {
                    SetOnseh(data.id.toString())
                }
                tv_getSMSCode2?.setOnClickListener {
                    val Inttem = Intent(context, OrderDetailsActivity().javaClass)
                    Inttem.putExtra("id", data.id)
                    context?.startActivity(Inttem)
                }

                Order_Dilis?.setOnClickListener {
                    val Init = Intent(context, DetailsActivity().javaClass)
                    Init.putExtra("msid", data.goods_id.toString())
                    Init.putExtra("Activity", "GatherFragment")
                    context?.startActivity(Init)
                }

            }

        }
        // 使用默认的加载更多的View。
        Special_recy?.useDefaultLoadMore()
        Special_recy?.loadMoreFinish(false, true)
        Special_recy?.setLoadMoreListener {
            page++
            OrderList()
        }
        Special_recy?.adapter = baseRecyclerAdapter

    }

    fun OrderList() {
        queryInfoPresenter?.netWorkRequestPost(HttpURL.Req_getOrderList,
            HttpRequest.getOrderList(is_team, status, page, 10))
    }


    override fun onDestroyView() {
        queryInfoPresenter?.detachView()
        super.onDestroyView()
    }

    override fun onSuccess(data: String?) {
        Log.e("GenerComFramnet", "is_team$is_team---" + "+status$status---" + data.toString())
        gGenerComFrBean = GsonUtils.getGson().fromJson(data, GenerComFramnetBean().javaClass)
        if (page == 1) {
            baseRecyclerAdapter?.clear()
        }
        if (gGenerComFrBean?.data?.list?.size!! > 0) {
            baseRecyclerAdapter?.setData(gGenerComFrBean?.data?.list)
            Special_recy?.loadMoreFinish(false, true)
        } else
            Special_recy?.loadMoreFinish(true, false)
    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }

    override val layoutId: Int
        get() = R.layout.genercom_framnet

    fun Setstatus(id: String) {
        queryInfoPresenter?.netWorkRequestPost(HttpURL.Req_finshOrder,
            HttpRequest.getGoodsDetail(id),
            object :
                TaskCallback {
                override fun onSuccess(data: String?) {
                    onResume()
                }

                override fun onFailure(
                    e: Throwable?,
                    code: Int,
                    msg: String?,
                    isNetWorkError: Boolean
                ) {

                }

            })

    }

    fun SetOnseh(id: String) {
        MessageDialog.Builder(context as FragmentActivity?)
            .setMessage("您是否取消订单？")
            .setCancel("确定")
            .setConfirm("再想想")
            .setListener(object : MessageDialog.OnListener {
                override fun onConfirm(dialog: Dialog?) {

                }

                override fun onCancel(dialog: Dialog?) {
                    queryInfoPresenter?.netWorkRequestPost(Req_addRefund,
                        HttpRequest.waitChange("id", id),
                        object : TaskCallback {
                            override fun onSuccess(data: String?) {
                                onResume()
                            }

                            override fun onFailure(
                                e: Throwable?,
                                code: Int,
                                msg: String?,
                                isNetWorkError: Boolean
                            ) {
                                ToastUtil.showMsg(msg)
                            }

                        })
                }

            }).show()
    }


}